<?php
include_once('./config/config.php');
if ($_POST) {
  $username = $_POST["username"];
  $password = $_POST["password"];
  $repass = $_POST["repass"];

  if (empty($username)) {
    ShowMsg('请输入用户名');
  }

  if (empty($password)) {
    ShowMsg('请输入密码');
  }
  if (empty($repass)) {
    ShowMsg('请确认密码');
  }
  if ($repass != $password) {
    ShowMsg('密码前后不一致，请重新输入');
  }

  $avatar = upload('avatar');
  if ($avtar['code'] === 0) {
    ShowMsg($avtar['msg']);
  }
  // 重新赋值
  // $avatar = $avatar['data'];
  // 生成密码盐
  $salt = build_ranstr();
  // md5加密密码跟密码盐拼接的结果
  $NewPass = md5($password . $salt);
  // 封装数组
  $data = [
    'username' => $username,
    'password' => $NewPass,
    'avatar' => $avatar['data'], // 图片 路径+文件名
    'salt' => $salt
  ];
  // $sql = "insert into `pre_admin`(`username`, `password`,`salt`,`avatar`)values('$username','$NewPass','$salt','$avatar')";
  // $result = mysqli_query($conn, $sql);
  // 调用插入函数
  $result = insert('admin', $data);

  if ($result['code'] === 1) {
    ShowMsg('新增成功', 'AdminList.php');
  } else {
    if (!empty($_FILES['avatar']) && $data['avatar']) {
      is_file($data['avatar']) && @unlink($data['avatar']);
    }
    echo $result['msg'];
    exit;
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入公共样式 -->
  <?php include_once('./common/meta.php'); ?>

  <!-- 引入公共js -->
  <?php include_once('./common/script.php'); ?>
</head>
<style>
  #img {
    width: 150px;
  }

  label[for="avatar"] {
    width: 150px;
  }
</style>

<body>
  <!-- 引入头部 -->
  <?php include_once('./common/header.php'); ?>

  <!-- 引入侧边菜单 -->
  <?php include_once('./common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">新增管理员</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">Index</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='AdminList.php'"><i class="icon-list"></i> 管理员列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>用户名</label>
                <input type="text" name="username" class="input-xxlarge" placeholder="请输入用户名">
                <label>密码</label>
                <input type="password" name="password" class="input-xxlarge" placeholder="请输入密码">
                <label>确认密码</label>
                <input type="password" name="repass" class="input-xxlarge" placeholder="请确认密码">
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden>
                <label for="avatar">
                  <img src="./assets/images/upload.png" alt="" id="img">
                </label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel">Delete Confirmation</h3>
          </div>
          <div class="modal-body">

            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?
            </p>
          </div>
          <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button class="btn btn-danger" data-dismiss="modal">Delete</button>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php'); ?>
      </div>
    </div>
  </div>

</body>

</html>

<script>
  $('#avatar').change(function() {
    var file = $(this)[0].files[0]

    var url = GetObjectURL(file)

    $('#img').attr('src', url)
  })
</script>